package com.example.onlinemonitoring.Util;

import org.apache.mina.core.session.IoSession;

import java.util.HashMap;
import java.util.Map;

public class SerialSessionUtil {
	public static Map<String,IoSession> sessionMap = new HashMap<>();
	
	public static void addSession(String key,IoSession session){
		sessionMap.put(key, session);
	}

    public static void removeSession(IoSession session){
        if(sessionMap.containsKey(session.getId())){
            sessionMap.remove(session.getId());
        }
    }
	
	public static IoSession getSession(String key){
		return sessionMap.get(key);
	}

	public static void sendMessageToAllSession(String message){
        sessionMap.forEach((id, session) -> sendMessage(session, message));
    }

	

	public static void sendMessage(IoSession session, String message){
	    if(session == null || session.isClosing()){
	        return;
        }
        session.write(message);
    }
	
}
